339C - Xenia and Weights - CodeForces Solution


constructive algorithms dfs and similar dp graphs greedy shortest paths *1700

Please click on ads to support us..

Python Code:

import sys
sys.setrecursionlimit(100000)
l=list(input())
m=int(input())
ans=[]
def fun(step,balance,weight):
    if step==m:
        return 1
    if balance>0:
        for i in range(len(l)):
            if i+1==weight:
                continue
            if l[i]=="1" and balance-i-1<0:
                ans.append(i+1)
                if fun(step+1,balance-i-1,i+1):
                    return 1
                ans.pop()
        return 0
    if balance<0:
        for i in range(len(l)):
            if i+1==weight:
                continue
            if (l[i]=="1" and balance+i+1>0):
                ans.append(i+1)
                if fun(step+1,balance+i+1,i+1):
                    return 1
                ans.pop()
        return 0
    if balance==0:
        for i in range(len(l)):
            if l[i]=="1":
                ans.append(i+1)
                if fun(step+1,i+1,i+1):
                    return 1
                ans.pop()
        return 0
if fun(0,0,0):
    print("YES")
    print(*ans)
else :
    print("NO")

    

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define long long long
#define INF ((long) 1e18)
#define MOD ((long) 1e9 + 7)
#define mll map<long, long>
#define pll pair<long, long>
#define pb push_back
#define FOR(i, a, b) \
  for (long i = (a); i < (long) (b); i++)
#define PRINTV(v) \
  FOR (_i, 0, v.size()) \
    cout << v[_i] << " "; \
  cout << "\n";

long f[1010][101][15];

long solve(long m, long x, long ant, string &s, auto &resp)
{
  if (m == 0) return 1;
  if (f[m][x][ant] != 0) return f[m][x][ant];
  FOR (i, 0, s.size()) {
    if (s[i] == '0') continue;
    long y = i+1;
    if (y > x and y != ant) {
      if (solve(m-1, y-x, y, s, resp) == 1) {
        f[m][x][ant] = 1;
        resp.pb(y);
        return 1;
      }
    }
  }
  f[m][x][ant] = -1;
  return -1;
}

int main()
{
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

  string s;
  long m;
  cin >> s >> m;

  vector<long> resp;
  long ok = solve(m, 0, -1, s, resp);
  if (ok == -1) puts("NO");
  else {
    cout << "YES" << endl;
    reverse(resp.begin(), resp.end());
    PRINTV(resp);
  }
}


Comments

Submit
0 Comments
More Questions

1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro